package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import artid.com.arttogoa.R;

import artid.com.arttogoa.adapter.FilingQuickSell_Adapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.FilingQuickSellParser;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout.OnRefreshListener;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullableListView;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：FilingQuickSell_Activity   
 * 类描述   ：备案一键出售   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-13 下午5:39:34   
 *
 */
public class FilingQuickSell_Activity extends BaseActivity implements OnClickListener{
	private Context mContext;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page=1;
	private FilingQuickSell_Adapter adapter;
	private EditText searchbox_Ed;
	private TextView searchbox_hint,searchbox_CancelBtn,noDataHint;
	private ImageView searchbox_icon;
	private LinearLayout noDataLL;
	private List<FilingQuickSellParser> list;
	private List<FilingQuickSellParser> more_list;
	private String keyword="";//搜索关键字
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filingquicksell);
		setContent(this, getIntent());
		mContext=FilingQuickSell_Activity.this;
		initView();
	}
	private void initView(){
		mTittle.setText("备案出售");
		setBack();
		list=new ArrayList<FilingQuickSellParser>();
		more_list=new ArrayList<FilingQuickSellParser>();
		//搜索框初始化组件
		searchbox_Ed=(EditText) findViewById(R.id.include_searchbox_EditText);
		searchbox_Ed.setOnFocusChangeListener(onFocusChange);
		searchbox_Ed.addTextChangedListener(mTextChangedListener);
		searchbox_hint=(TextView) findViewById(R.id.include_searchbox_hint);
		searchbox_icon=(ImageView) findViewById(R.id.include_searchbox_icon);
		searchbox_CancelBtn=(TextView) findViewById(R.id.include_searchbox_CancelBtn);
		searchbox_CancelBtn.setOnClickListener(this);
		noDataLL=(LinearLayout) findViewById(R.id.include_noDataLL);
		noDataHint=(TextView) findViewById(R.id.include_noData_hint);
		noDataHint.setText("哎呦，没有备案哦！");
		mListView =(PullableListView) findViewById(R.id.filingquicksell_listivew);
		mPullLayout = (PullToRefreshLayout)findViewById(R.id.filingquicksell_refresh_view);
		mPullLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				//下拉刷新
				page=1;
				isRefreshed=true;
				getFilingList(keyword,false);
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				//上拉加载
				page++;
				getFilingList(keyword,false);
			}
		}); 
		getFilingList(keyword,true);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.include_searchbox_CancelBtn:
			//搜索框取消按钮
			keyword="";//清空搜索关键字
			searchbox_Ed.setText("");
			searchbox_Ed.clearFocus();
			break;
		default:
			break;
		}
	}

	/**
	 * 搜索框焦点监听
	 */
	OnFocusChangeListener onFocusChange=new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			if(hasFocus){
				//获得焦点  
				searchbox_hint.setVisibility(View.GONE);
				searchbox_icon.setVisibility(View.VISIBLE);
				searchbox_CancelBtn.setVisibility(View.VISIBLE);
			}else{
				//失去焦点  
				searchbox_hint.setVisibility(View.VISIBLE);
				searchbox_icon.setVisibility(View.GONE);
				searchbox_CancelBtn.setVisibility(View.GONE);
				hideSoftInput(FilingQuickSell_Activity.this);
			}  
		}
	};

	/**
	 * 对EditText监听
	 */
	TextWatcher mTextChangedListener = new TextWatcher() {
		private CharSequence temp;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
			temp = s;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			// mTextView.setText(s);//将输入的内容实时显示
		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			keyword=searchbox_Ed.getText().toString();
			page=1;
			isRefreshed=true;
			getFilingList(keyword,false);
		}
	};

	/**
	 * 关闭软键盘
	 * @param mContext
	 */
	/**
	 * 隐藏软键盘
	 */
	@SuppressWarnings("static-access")
	public static void hideSoftInput(Activity activity){
		/**隐藏软键盘**/
		View view = activity.getWindow().peekDecorView();
		if (view != null) {
			InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
			inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}

	/**
	 * 获取备案列表（这里与首页的艺术品备案接口通用:这里应该请求“2”：审核通过的）
	 */
	private void getFilingList(String keyword,Boolean isShowLoding){
		VolleyBasic.getInstance(FilingQuickSell_Activity.this).post(isShowLoding,isShowLoding,Cache.recordlist,
				VolleyMap.getInstance().recordList("2", page+"","10",keyword),
				FilingQuickSellParser.class, new VolleyCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				list=(List<FilingQuickSellParser>) vd.getList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}
				if(more_list!=null && more_list.size()!=0){
					//这里是防止登陆的情况下增加关注数量
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
				if (adapter == null) {
					adapter=new FilingQuickSell_Adapter(mContext,more_list);
					mListView.setAdapter(adapter);
				}
				adapter.notifyDataSetChanged();
				Log.d("log", more_list.toString());
			}
			public void onError(VolleyError volleyError) {
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
				if(more_list!=null && more_list.size()!=0){
					//这里是防止登陆的情况下增加关注数量
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
			}
		});
	}

}
